/*
 * ShopDetailPresenter.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shopdetail;

import android.os.Bundle;

import com.yidiandao.business.R;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.Shop;
import com.yidiandao.business.data.model.IShopModel;
import com.yidiandao.business.data.response.QueryShopDetailHeaderResponse;
import com.yidiandao.business.utils.UIUtils;

/** 店铺详情页-Presenter */
public class ShopDetailPresenter implements ShopDetailContract.Presenter {

    private ShopDetailContract.View mShopDetailView;

    private IShopModel mShopModel;

    private boolean mHasCollection;

    private String mSupplierId;

    public ShopDetailPresenter(ShopDetailContract.View view, IShopModel shopModel) {
        mShopDetailView = view;
        mShopModel = shopModel;

        mShopDetailView.setPresenter(this);
    }

    @Override
    public void start() {
    }

    @Override
    public void loadHeader(Bundle bundle) {

        //        if (TextUtils.isEmpty(shop.getShopName())) {
        // 表示来自商品列表 需要从网络加载顶部数据

        mSupplierId = bundle.getString(Intents.EXTRA_SUPPLIER_ID);

        loadShopDetailHeader(mSupplierId);

        //        } else {
        // 显示店家评分
        //            mShopDetailView.setQualityLevel(shop.getCommentRand());
        //            mShopDetailView.setServiceLevel(shop.getServer());
        //            mShopDetailView.setLogisticsLevel(shop.getShipping());
        //
        //            mShopDetailView.refreshShopHeader(shop.getShopLogo(), shop.getTopAdver(),
        // shop.getShopName(), shop.getGoodsNumber(), shop.getSelesNum());
        //    }

    }

    public void loadShopDetailHeader(String supplierId) {
        mShopModel.loadShopDetailHeader(
                supplierId,
                mShopDetailView.getPagerTag(),
                new IShopModel.OnLoadShopHeaderListener() {
                    @Override
                    public void onSuccess(QueryShopDetailHeaderResponse.Data data) {

                        // 店铺名称
                        mShopDetailView.refreshShopName(data.getShopName());

                        // 店铺是否已经被收藏
                        mHasCollection = data.getHasCollection() == Shop.HAS_COLLECTION;
                        mShopDetailView.setHasCollection(mHasCollection);

                        // 显示店家评分
                        mShopDetailView.setQualityLevel(data.getAvgComment());
                        mShopDetailView.setServiceLevel(data.getServer());
                        mShopDetailView.setLogisticsLevel(data.getShipping());

                        // 店铺公告
                        String shopNotice = data.getShopNotice();

                        mShopDetailView.refreshShopHeader(
                                data.getShopLogo(),
                                data.getTopAdver(),
                                data.getGoodsNumber(),
                                data.getSalesNum(),
                                shopNotice);
                    }

                    @Override
                    public void onFailed() {
                    }
                });
    }

    @Override
    public void collectShop() {
        mShopModel.collectShop(
                mSupplierId,
                !mHasCollection,
                mShopDetailView.getPagerTag(),
                new IShopModel.OnAccessListener() {
                    @Override
                    public void onSuccess() {
                        UIUtils.showToast(
                                UIUtils.getString(
                                        mHasCollection
                                                ? R.string.shop_cancel_collect_success
                                                : R.string.shop_collect_success));
                        mShopDetailView.setHasCollection(!mHasCollection);
                        mHasCollection = !mHasCollection;
                    }
                });
    }
}
